<?php
namespace app\index\controller;

use app\common\controller\Base;
use app\common\model\SscKjjglist;
use app\common\logic\Hk6;
use think\Db;

class Lottery extends Base
{
    protected $hk6_logic = [];
    protected $ssckjjglist = [];
    protected $hezhi_boundary = 175;

    public function _initialize()
    {
        parent::_initialize();
        $start = [
            'user_all' => $this->user_all,
            'app_down' => $this->app_down,
            'logo' => $this->logo
        ];
        $this->ssckjjglist = new SscKjjglist();
        $this->hk6_logic = new Hk6();
        $this->assign($start);
    }

    /**
     * @Purpose 电脑版开奖公告
     * @Time 2017-11
     */
    public function runLottery()
    {
        //彩种、期号、日期对应数字等参数
        $czid = input('czid', '');
        $qishu = input('qishu', '');
        $uuid = input('uuid', '');
        $timebh = input('timebh', '');
        $sql_shert = $qishu ? " AND qihao='{$qishu}'" : '';
        //彩票种类列表
        $lottery_type = $this->lotteryTypeList();
        //电脑版广告
        $pc_ad = $this->ADForPC();
        //彩种大分类列表
        $lotterys = Db::name('lottery_classify')->field('id,czids,class_name_lottery,pic')->where('status', 1)->select();
        //根据大分类确定小分类
        if (!$uuid) {
            $kj_list = Db::name('caipai_list')->field('id,id,cp_name_old,cp_name_new,cpclass')->where('cp_if_open', '<', 2)->order('cp_dn_px', 'DESC')->select();
        } else {
            //低频彩单独分类
            if ($uuid == 6) {
                $kj_list = Db::name('caipai_list')->field('id,cp_name_old,cp_name_new,cpclass')->where('cpclass', 2)->where('cp_if_open', '<', 2)->select();
            } else {
                $czids = Db::name('lottery_classify')->where('id', $uuid)->value('czids');
                $kj_list = Db::name('caipai_list')->field('id,cp_name_old,cp_name_new,cpclass')->where("id IN ({$czids})")->where('cp_if_open', '<', 2)->select();
            }
        }
        //给大分类附加初始czid
        if (!$czid) {
            $czid = $kj_list[0]['id'];
        }
        //根据日期查询开奖信息
        $KjjgList = new \app\index\logic\KjjgList();
        $kjjg_list = $KjjgList->getRunLotteryWithDay($czid, $timebh, $sql_shert);
        $this->assign('czid', $czid);
        $this->assign('uuid', $uuid);
        $this->assign($lottery_type);
        $this->assign($pc_ad);
        $this->assign('lotterys', $lotterys);
        $this->assign('kj_list', $kj_list);
        $this->assign('echo_kjjg_list', $kjjg_list);
        return $this->fetch('run_lottery');
    }

    /**
     * @Purpose 电脑版走势
     * @Time 2017-11
     */
    public function trend()
    {
        $czid = $firstid = input('czid', '');
        $uuid = input('uuid', '');
        $uuid = ($uuid == 10) ? '' : $uuid;

        //判断彩种是否开启
        if (($czid && $this->lottery_if_open($czid) != 1) || $czid == 60 || $czid == 48) {
            echo '<script>alert("抱歉，您无权访问该页面！");history.go(-1);</script>';
            exit;
        }

        //彩票种类列表
        $lottery_type = $this->lotteryTypeList();
        //电脑版广告
        $pc_ad = $this->ADForPC();
        //彩种大分类列表
        $lotterys = Db::name('lottery_classify')->field('id,czids,class_name_lottery,pic')->where('status', 1)->where('id', '<>', '10')->select();
        //根据大分类确定小分类
        if (!$uuid) {
            $kj_list = Db::name('caipai_list')->field('id,id,cp_name_old,cp_name_new,cpclass')->where('cp_if_open', '<', 2)->where('id', ['<>', '48'], ['<>', '60'], 'and')->order('cp_dn_px', 'DESC')->select();
        } else {
            //低频彩单独分类
            if ($uuid == 6) {
                $kj_list = Db::name('caipai_list')->field('id,cp_name_old,cp_name_new,cpclass')->where('cpclass', 2)->where('cp_if_open', '<', 2)->select();
            } else {
                $czids = Db::name('lottery_classify')->where('id', $uuid)->value('czids');
                $kj_list = Db::name('caipai_list')->field('id,cp_name_old,cp_name_new,cpclass')->where("id IN ({$czids})")->where('cp_if_open', '<', 2)->select();
            }
        }
        //给大分类附加初始czid
        if (!$czid) {
            $czid = $kj_list[0]['id'];
        }
        $this->assign('czid', $czid);
        $this->assign('uuid', $uuid);
        $this->assign($lottery_type);
        $this->assign($pc_ad);
        $this->assign('lotterys', $lotterys);
        $this->assign('kj_list', $kj_list);
        $first = array();
        if (empty($firstid)) {
            $first = array_shift($kj_list);
        } else {
            foreach ($kj_list as $v) {
                if (intval($v['id']) == intval($firstid)) $first = $v;
            }
        }
        $this->assign('first', $first);
        return $this->fetch('trend');
    }

    /**
     * @Author  wpy
     * @Purpose 六合彩走势
     * @Time 2017-15-6
     */
    public function liuhecai()
    {
        $czid = 9;
        $uuid = input('uuid', '全部');
        //彩票种类列表
        $lottery_type = $this->lotteryTypeList();
        //电脑版广告
        $pc_ad = $this->ADForPC();
        //彩种大分类列表
        $lotterys = Db::name('lottery_classify')->field('id,czids,class_name_lottery,pic')->where('status', 1)->where('id', '<>', '10')->select();
        $fileds = "*";
        $where = array(
            'czid' => 9,
            'kjjg' => array('gt', '0')
        );
        $order = 'qihao desc';
        $limit = '30';
        $data_lists = $this->ssckjjglist->JiLu($fileds, $where, $order, $limit);

        foreach ($data_lists as &$data_single) {
            $data = $data_single['kjjg'];
            if ($data != '0') {
                $tema = substr($data, 12);
                $temawei = substr($data, 13);
                $arr = str_split($data, 2);
                $data_single['kjjg'] = implode(',', $arr);
                $data_single['dat_codes_array'] = $this->hk6_logic->dataFormat($data_single['kjjg']);
                $bose = array();
                foreach ($data_single['dat_codes_array'] as $val) {
                    $bose[] = $val['bose'];
                }

                $bose = array_count_values($bose);
                $bose_max = max($bose);
                $data_single['qisebo'] = array_search($bose_max, $bose);

                $data_single['jztime'] = date('m-d H:i:s', $data_single['jztime']);
                $data_single['sum'] = array_sum(getCodeArr($data_single['kjjg']));
                $data_single['sum'] % 2 == '0' ? $data_single['danshuang'] = '双' : $data_single['danshuang'] = '单';
                $data_single['sum'] >= $this->hezhi_boundary ? $data_single['daxiao'] = '大' : $data_single['daxiao'] = '小';
                $data_single['temadanshuang'] = getEvenOrOdd($tema);
                $data_single['temadaxiao'] = getBigOrSmall($tema, 25, 49);
                $data_single['temadaxiaowei'] = getBigOrSmall($temawei, '4', '');

                unset($data_single['kjjg']);
            }
        }
        $this->assign('uuid', $uuid);
        $this->assign('data_lists', $data_lists);
        $this->assign('lotterys', $lotterys);
        $this->assign('czid', $czid);
        $this->assign($lottery_type);
        $this->assign($pc_ad);
        return $this->fetch();
    }

    public function liuhecaiSimple()
    {
        $czid = 9;
        $uuid = input('uuid', '');
        //彩票种类列表
        $lottery_type = $this->lotteryTypeList();
        //电脑版广告
        $pc_ad = $this->ADForPC();
        //彩种大分类列表
        $lotterys = Db::name('lottery_classify')->field('id,czids,class_name_lottery,pic')->where('status', 1)->where('id', '<>', '10')->select();
        $fileds = "*";
        $where = array(
            'czid' => 9,
            'kjjg' => array('gt', '0')
        );
        $order = 'qihao desc';
        $limit = '30';
        $data_lists = $this->ssckjjglist->JiLu($fileds, $where, $order, $limit);

        foreach ($data_lists as &$data_single) {
            $data = $data_single['kjjg'];
            if ($data != '0') {
                $tema = substr($data, 12);
                $temawei = substr($data, 13);
                $arr = str_split($data, 2);
                $data_single['kjjg'] = implode(',', $arr);
                $data_single['dat_codes_array'] = $this->hk6_logic->dataFormat($data_single['kjjg']);
                $bose = array();
                foreach ($data_single['dat_codes_array'] as $val) {
                    $bose[] = $val['bose'];
                }

                $bose = array_count_values($bose);
                $bose_max = max($bose);
                $data_single['qisebo'] = array_search($bose_max, $bose);

                $data_single['jztime'] = date('m-d H:i:s', $data_single['jztime']);
                $data_single['sum'] = array_sum(getCodeArr($data_single['kjjg']));
                $data_single['sum'] % 2 == '0' ? $data_single['danshuang'] = '双' : $data_single['danshuang'] = '单';
                $data_single['sum'] >= $this->hezhi_boundary ? $data_single['daxiao'] = '大' : $data_single['daxiao'] = '小';
                $data_single['temadanshuang'] = getEvenOrOdd($tema);
                $data_single['temadaxiao'] = getBigOrSmall($tema, 25, 49);
                $data_single['temadaxiaowei'] = getBigOrSmall($temawei, '4', '');

                unset($data_single['kjjg']);
            }
        }
        $this->assign('uuid', $uuid);
        $this->assign('data_lists', $data_lists);
        $this->assign('lotterys', $lotterys);
        $this->assign('czid', $czid);
        $this->assign($lottery_type);
        $this->assign($pc_ad);
        return $this->fetch('liuhecai_simple');
    }


    /**
     * @Purpose 电脑版走势图表
     * @Time 2017-11
     */
    public function trendPic()
    {
        $czid = input('czid');
        $czid = isset($czid) ? $czid : 1;

        //彩票种类列表
        $lottery_type = $this->lotteryTypeList();
        //电脑版广告
        $pc_ad = $this->ADForPC();

        $this->assign($lottery_type);
        $this->assign($pc_ad);
        $this->assign('czid', $czid);
        return $this->fetch('trend_pic');
    }

    /**
     * @Purpose 电脑版走势框架集
     * @Time 2017-11
     */
    public function trendIframe()
    {
        //接收参数
        $czid = input('czid', '');
        $hmq = input('hmq', '');
        if (!$czid || !$hmq) exit;
        //生成走势图表
        $KjjgList = new \app\index\logic\KjjgList();
        $data = $KjjgList->getTrendJson($czid, $hmq);
        
        //输出数据
        echo $data;
    }

}